package com.topvision.opencv_test;

import android.os.Handler;
import android.os.Message;

import org.opencv.core.Rect;

/**
 * Author: lucy
 * Comment: //TODO
 * Date: 2017-10-11 11:03
 */

public class DrawRectHandler extends Handler {
    private int lastX;
    private int lastY;
    private int pointCount;
    private DrawRectView mDrawRectView;
    private MainActivity mMainActivity;

    public DrawRectHandler(DrawRectView drawRectView, MainActivity mainActivity) {
        mDrawRectView = drawRectView;
        mMainActivity = mainActivity;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                Rect rect = (Rect) msg.obj;
                //二维码的长宽容错率
                if (Math.abs(rect.height - rect.width) <= 5) {
                    mDrawRectView.drawRect(rect);
//                    mMainActivity.zoomIndex += 5;
//                    mMainActivity.setZoom(mMainActivity.zoomIndex);
                }
//                if (Math.abs(rect.width - 2f * rect.height) <= rect.height) {
//                    mDrawRectView.drawRect(rect);
//                    mMainActivity.zoomIndex += 2;
//                    mMainActivity.setZoom(mMainActivity.zoomIndex);
//                }
                System.out.println("X：" + rect.x + ", Y：" + rect.y + ", Width：" + rect.width + ", Height：" + rect.height);
                //根据x,y当前识别到的坐标点，模糊判断是否是二维码
                if (Math.abs(rect.x - lastX) <= 10 && Math.abs(rect.y - lastY) <= 10) {
                    //2次都在一个位置,记录一次
                    pointCount++;
                } else {
                    pointCount = 0;
                }
                if (pointCount == 6) {
                    //模糊肯定，追踪到了二维码
                    ToastUtils.showToast(mMainActivity, "监测的可能存在二维码，请靠近识别");
                    //清空检测数据，避免跳转到图像识别
                    pointCount = 0;
                    mMainActivity.indexCount = 0;
                }
                //记录前面一次x,y坐标
                lastX = rect.x;
                lastY = rect.y;
                break;
        }
    }
}
